package com.example.springMVC.controller;

import com.example.springMVC.commons.Result;
import com.example.springMVC.commons.ServiceException;
import com.example.springMVC.pojo.Bill;
import com.example.springMVC.pojo.User;
import com.example.springMVC.service.BillService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.sql.Date;

@Controller
@RequestMapping("/bill")
public class BillController {
    @Autowired
    BillService billService;

    @ResponseBody
    @RequestMapping("billList")
    public Result billList(Integer currentPage, @RequestParam(required = false) Integer proId, @RequestParam(required = false) Integer isPayment, @RequestParam(required = false) String proName) {
        PageInfo<Bill> billList = billService.billList(currentPage, proId, isPayment, proName);
        return Result.success(billList);
    }

    @ResponseBody
    @RequestMapping("billAdd")
    public Result billAdd(Bill bill, HttpSession session) throws ServiceException {
        User loginUser = (User) session.getAttribute("user");
        bill.setCreatedBy(loginUser.getId());
        bill.setCreationDate(new Date(new java.util.Date().getTime()));
        boolean fale = billService.billAdd(bill);
        return Result.success(fale);
    }

    @ResponseBody
    @DeleteMapping("{billId}")
    public Result deleteBill(@PathVariable Long billId) {
        boolean fale = billService.deleteBill(billId);
        return Result.success(fale);
    }

    @ResponseBody
    @GetMapping("{billId}")
    public Result getBillById(@PathVariable long billId) {
        Bill bill = billService.getUserById(billId);
        return Result.success(bill);
    }

    @ResponseBody
    @PostMapping("{billId}")
    public Result updateBillById(@PathVariable long billId ,Bill bill){
        bill.setId(billId);
        boolean fale = billService.updateBillById(bill);
        return Result.success(fale);
    }
}
